﻿using System.Data;
using System.Linq;
using System.Web.Mvc;
using Warehouse.Models;

namespace Warehouse.Controllers
{ 
    public class SensorsController : Controller
    {
        private readonly WarehouseEntities _db = new WarehouseEntities();

        //
        // GET: /Sensors/

        public ViewResult Index()
        {
            var sensors = _db.Sensors.Include("MeasurePoint").Include("ModelDevice").Include("TypeDevice");
            return View(sensors.ToList());
        }

        public ViewResult Details(int id)
        {
            Sensors sensors = _db.Sensors.Single(s => s.ID == id);
            return View(sensors);
        }

        public ActionResult Create()
        {
            ViewBag.MeasurePointID = new SelectList(_db.MeasurePoint, "ID", "Title");
            ViewBag.ModelDeviceID = new SelectList(_db.ModelDevice, "ID", "Title");
            ViewBag.TypeDeviceID = new SelectList(_db.TypeDevice, "ID", "Title");
            return View();
        } 

        [HttpPost]
        public ActionResult Create(Sensors sensors)
        {
            if (ModelState.IsValid)
            {
                _db.Sensors.AddObject(sensors);
                _db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.MeasurePointID = new SelectList(_db.MeasurePoint, "ID", "Title", sensors.MeasurePointID);
            ViewBag.ModelDeviceID = new SelectList(_db.ModelDevice, "ID", "Title", sensors.ModelDeviceID);
            ViewBag.TypeDeviceID = new SelectList(_db.TypeDevice, "ID", "Title", sensors.TypeDeviceID);
            return View(sensors);
        }

        public ActionResult Edit(int id)
        {
            Sensors sensors = _db.Sensors.Single(s => s.ID == id);
            ViewBag.MeasurePointID = new SelectList(_db.MeasurePoint, "ID", "Title", sensors.MeasurePointID);
            ViewBag.ModelDeviceID = new SelectList(_db.ModelDevice, "ID", "Title", sensors.ModelDeviceID);
            ViewBag.TypeDeviceID = new SelectList(_db.TypeDevice, "ID", "Title", sensors.TypeDeviceID);
            return View(sensors);
        }

        [HttpPost]
        public ActionResult Edit(Sensors sensors)
        {
            if (ModelState.IsValid)
            {
                _db.Sensors.Attach(sensors);
                _db.ObjectStateManager.ChangeObjectState(sensors, EntityState.Modified);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.MeasurePointID = new SelectList(_db.MeasurePoint, "ID", "Title", sensors.MeasurePointID);
            ViewBag.ModelDeviceID = new SelectList(_db.ModelDevice, "ID", "Title", sensors.ModelDeviceID);
            ViewBag.TypeDeviceID = new SelectList(_db.TypeDevice, "ID", "Title", sensors.TypeDeviceID);
            return View(sensors);
        }

        public ActionResult Delete(int id)
        {
            Sensors sensors = _db.Sensors.Single(s => s.ID == id);
            return View(sensors);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Sensors sensors = _db.Sensors.Single(s => s.ID == id);
            _db.Sensors.DeleteObject(sensors);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}